package com.teach;

import java.util.HashMap;
import java.util.Map;

public class MapTest {

	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<Integer, String>();
		//加入数据key-value
		map.put(1, "张三");
		map.put(2, "李四");
		map.put(3, "王五");
		System.out.println(map);
		map.put(1,"张三丰");
		map.put(4, "王五");
		System.out.println(map);//key不重复的，value可重复的
		map.put(null,"张三");
		map.put(null,"李四");
		System.out.println(map);//key可以为null，但只能有一个
		map.put(null,null);
		map.put(1, null);
		System.out.println(map);//value可以为null，可以有多个
		
//		map.clear();
//		System.out.println(map);//清空key-value
		//key是否存在
		System.out.println("map.containsKey(1):"+map.containsKey(1));
		System.out.println("map.containsKey(-1):"+map.containsKey(-1));
		//value是否存在
		System.out.println("map.containsValue(\"李四\"):"+map.containsValue("李四"));
		System.out.println("map.containsValue(\"张三\"):"+map.containsValue("张三"));
		
		//根据key获取value
		System.out.println("map.get(2)="+map.get(2));
		System.out.println("map.get(-1)="+map.get(-1));
		System.out.println("map.getOrDefault(2,\"默认值\")="+map.getOrDefault(2,"默认值"));
		System.out.println("map.getOrDefault(-1,\"默认值\")="+map.getOrDefault(-1,"默认值"));
		
		//是否为空
		System.out.println("map.isEmpty()="+map.isEmpty());
		
		//key-value的数量
		System.out.println("map.size()="+map.size());
		
		//删除key-value
		System.out.println("map.remove(4)="+map.remove(4));
		System.out.println(map);
		System.out.println("map.remove(1, \"张三丰\")="+map.remove(1, "张三丰"));
		System.out.println("map.remove(2, \"李四\")="+map.remove(2, "李四"));
		System.out.println(map);
	}

}
